
//
//  ZZHomeView.m
//  新项目
//
//  Created by 刘耀宗 on 2016/12/21.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

#import "ZZHomeView.h"
#import "JHChartHeader.h"
#import "ZZOrderHeaderView.h"
#import "ZZOrderHeaderViewCell.h"
#import "WBPopMenuModel.h"
#import "WBPopMenuSingleton.h"
#import "ZZTimeModel.h"
#import "ZZOrderViewModel.h"
#import "ZZSettingVC.h"
#import "ZZOrderModel.h"
#import "ZZOrderContentModel.h"
#import "ZZOrderPopView.h"
#import "WBPopMenuView.h"
#import "ZZCSTimeView.h"
#define k_MainBoundsWidth [UIScreen mainScreen].bounds.size.width
#define k_MainBoundsHeight [UIScreen mainScreen].bounds.size.height



@interface ZZHomeView ()<ZZCSTimeViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (nonatomic, strong) JHRingChart *ring;
@property (weak, nonatomic) IBOutlet UIButton *moneyZBBtn;
@property (weak, nonatomic) IBOutlet UIButton *orderZBBtn;

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *mutaArr;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)NSString *currentMonthStr;
@property (nonatomic, strong) CcNoDataView *noDataView;

@property(nonatomic,strong)ZZOrderPopView *popView;

@property(nonatomic,assign) BOOL firstLoad;
@property (weak, nonatomic) IBOutlet UIButton *chooseTimeBtn;
@property (nonatomic, strong) ZZCSTimeView *timeView;
@property (nonatomic, strong) UIButton *coverBtn;



@end
@implementation ZZHomeView
-(UIButton *)coverBtn
{
    if (_coverBtn == nil) {
        _coverBtn = [[UIButton alloc] init];
        _coverBtn.backgroundColor=[UIColor blackColor];
        _coverBtn.alpha=0.4;
        [_coverBtn addTarget:self action:@selector(removeCoverBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _coverBtn;

}
-(ZZCSTimeView *)timeView
{
    if (_timeView == nil) {
        _timeView = [ZZCSTimeView ViewWithXib];
        _timeView.delegate=self;
    }
    return _timeView;

}
-(JHRingChart *)ring
{
    if (_ring == nil) {
        _ring = [[JHRingChart alloc] initWithFrame:CGRectMake(0, 290+45, k_MainBoundsWidth, 250)];
            [self addSubview:_ring];
    }
    return _ring;
    
}
+(instancetype)ViewWithXib
{
    ZZHomeView *view=[[[NSBundle mainBundle] loadNibNamed:@"ZZHomeVC" owner:nil options:nil] lastObject];
    return view;
    

}
//代理方法,选择
-(void)chooseTimeWithStartTime:(NSString *)startTime andEndTime:(NSString *)endTime
{ [self removeCoverBtn];
    if ([self.delegate respondsToSelector:@selector(changeTheTimeForSELETECTWithStartTime:andEndTime:)]) {
        [self.delegate changeTheTimeForSELETECTWithStartTime:startTime andEndTime:endTime];
    }

    

}
//关闭
-(void)removeCoverBtn
{
    [self.coverBtn removeFromSuperview];
    [self.timeView removeFromSuperview];
    

}
//移除

//点击切换占比
- (IBAction)clickForZb:(id)sender {
    UIButton *btn=(UIButton *)sender;
    //金额占比
    if (btn.tag==98) {
        [self.orderZBBtn setBackgroundColor:CXBColor(202, 202, 202)];
        [self.moneyZBBtn setBackgroundColor:CXBColor(20, 107, 170)];
    }
    //订单占比
    else
    {
        [self.moneyZBBtn setBackgroundColor:CXBColor(202, 202, 202)];
        [self.orderZBBtn setBackgroundColor:CXBColor(20, 107, 170)];
    }
    
    
}
- (IBAction)clickForChangeTime:(id)sender {
    if ([self.timeView isDescendantOfView:self]) {
        [self.timeView removeFromSuperview];
        
    }else
    {
        [self popDateMenu];
    }
    
    
    
}
- (NSArray *) titles {
    DLog(@"%@",_mutaArr);
    
    return _mutaArr;
}

-(void)popDateMenu
{
//    滚动
//    /*
//     if ([self.delegate respondsToSelector:@selector(scrollGunDong)]) {
//     [self.delegate scrollGunDong];
//     }
//     */
    
//    if(newwidth <= 320){
   

        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        CGRect  rect = [self.chooseTimeBtn.superview convertRect:self.chooseTimeBtn.frame toView:window];
        [[UIApplication sharedApplication].keyWindow addSubview:self.coverBtn];
        self.coverBtn.frame=CGRectMake(0, 0, newwidth, newheight);
        [[UIApplication sharedApplication].keyWindow addSubview:self.timeView];
         self.timeView.frame=CGRectMake(rect.origin.x-100, 260-84+90, 120, 110);
        self.timeView.backgroundColor=[UIColor clearColor];
//    }else{
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    CGRect rect = [self.chooseTimeBtn.superview convertRect:self.chooseTimeBtn.frame toView:window];
//    [[UIApplication sharedApplication].keyWindow addSubview:self.coverBtn];
//    self.coverBtn.frame=CGRectMake(0, 0, newwidth, newheight);
//    [[UIApplication sharedApplication].keyWindow addSubview:self.timeView];
//        
//self.timeView.frame=CGRectMake(rect.origin.x-100, rect.origin.y + rect.size.height, 120, 110);
//    self.timeView.backgroundColor=[UIColor clearColor];
//    }
    if ([self.delegate respondsToSelector:@selector(scrollGunDong)]) {
        [self.delegate scrollGunDong];
    }
}



-(void)returnSixYearAndMonth
{
    // 获取当前秒
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSTimeInterval time1970 = [now timeIntervalSince1970];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
    //    NSUInteger numberOfDaysInMonth = range.length;
    NSUInteger numberOfDaysInMonth = 30;
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 10 ; i++) {
        
        // 格式化时间
        NSDate *dateTime = [NSDate dateWithTimeIntervalSince1970:time1970-(86400*numberOfDaysInMonth*i)];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"yyyy年MM月"];
        [dateFormat stringFromDate:dateTime];
        
        NSLog(@"%@",dateTime);
        
        //得到近6月时间
        NSString *string = [NSString stringWithFormat:@"%@",dateTime];
        NSLog(@"%@",string);
        ZZTimeModel *model = [[ZZTimeModel alloc]init];
        model.year = [string substringToIndex:4];
        model.month = [string substringWithRange:NSMakeRange(5, 2)];
        NSString *timeStr = [NSString stringWithFormat:@"%@年%@月",model.year,model.month];
        NSLog(@"%@",timeStr);
        [arr addObject:timeStr];
        _mutaArr = arr;
    }
    
    
}



//设置圆角
-(void)setBtnRollWithBtn:(UIButton *)btn
{
    btn.layer.cornerRadius=14;
    btn.layer.masksToBounds=YES;
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    self.lineView.hidden=YES;
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //设置虚线颜色
    CGContextSetStrokeColorWithColor(currentContext, LineColor.CGColor);
    //设置虚线宽度
    CGContextSetLineWidth(currentContext, 2);
    //设置虚线绘制起点
    CGContextMoveToPoint(currentContext, self.lineView.frame.origin.x, self.lineView.frame.origin.y);
    //设置虚线绘制终点
    CGContextAddLineToPoint(currentContext, self.lineView.frame.origin.x,self.lineView.frame.origin.y + self.lineView.frame.size.height);
    //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
    CGFloat arr[] = {3, 1};
    //下面最后一个参数“2”代表排列的个数。
    CGContextSetLineDash(currentContext, 0, arr, 2);
    //画线
    CGContextDrawPath(currentContext, kCGPathStroke);

}
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setBtnRollWithBtn:self.moneyZBBtn];
    [self setBtnRollWithBtn:self.orderZBBtn];
    
   }
-(void)setZhanBiArr:(NSArray *)zhanBiArr
{
    _zhanBiArr=zhanBiArr;
    /*        background color         */
    self.ring.backgroundColor = [UIColor whiteColor];
    /*        Data source array, only the incoming value, the corresponding ratio will be automatically calculated         */
    self.ring.valueDataArr = zhanBiArr;
    /*         Width of ring graph        */
    self.ring.ringWidth = 35.0;
    /*        Fill color for each section of the ring diagram         */
    self.ring.fillColorArray = @[[UIColor colorWithRed:1.000 green:0.783 blue:0.371 alpha:1.000], [UIColor colorWithRed:1.000 green:0.562 blue:0.968 alpha:1.000],[UIColor colorWithRed:0.313 green:1.000 blue:0.983 alpha:1.000],[UIColor colorWithRed:0.560 green:1.000 blue:0.276 alpha:1.000],[UIColor colorWithRed:0.239 green:0.651 blue:0.170 alpha:1.000],[UIColor colorWithRed:0.239 green:0.651 blue:0.170 alpha:1.000]];
        [self.ring showAnimation];
}
//环状图
- (void)showRingChartView{
    
 
    /*        Start animation             */
//    [self.ring showAnimation];

}

//查看金额占比
- (IBAction)cliciForMoney:(id)sender {
    UIButton *btn=sender;
    if (!btn.selected) {
        btn.selected=!btn.selected;
        [btn setBackgroundColor:CcColor(50, 136, 236)];
        self.moneyButton.selected=NO;
        [self.moneyButton setBackgroundColor:CcColor(202, 202, 202)];
        if ([self.delegate respondsToSelector:@selector(lookMoneyZB:)]) {
            [self.delegate lookMoneyZB:self];
        }
    }
    
    
    
}
//查看订单占比
- (IBAction)clickForOrder:(id)sender {
    UIButton *btn=sender;
    if (!btn.selected) {
        btn.selected=!btn.selected;
        [btn setBackgroundColor:CcColor(50, 136, 236)];
        self.orderButton.selected=NO;
        [self.orderButton setBackgroundColor:CcColor(202, 202, 202)];
        if ([self.delegate respondsToSelector:@selector(lookOrderZB:)]) {
            [self.delegate lookOrderZB:self];
        }
    }
    
}
- (IBAction)clickForPushOrderDetail:(UIButton *)btn {

    if ([self.delegate respondsToSelector:@selector(toPushOrderDetail:withTag:)]) {
        [self.delegate toPushOrderDetail:self withTag:btn.tag];
    }

}


@end
